#include <stdafx.h>
#include "ScriptEditorSolutionItem.h"
#include "ScriptEditorSolution.h"
#include "ScriptEditorWidget.h"
#include "ScriptEditorTabWidget.h"


namespace Lit
{
	namespace Editor
	{
		ScriptSolutionItem::ScriptSolutionItem(ScriptSolutionPtr parent, size_t flags ): mSolution(parent), mFlags( flags ){}
		ScriptSolutionItem::ScriptSolutionItem( ScriptSolutionPtr parent, const NodePtr& node, const WidgetPtr& window, size_t flags ) : mSolution(parent),mNode(node), mFlags(flags), mEditor(window){ setFlags(flags); }
		ScriptSolutionItem::ScriptSolutionItem( const ScriptSolutionItem& rk ) : mNode(rk.mNode), mSolution(rk.mSolution), mEditor(rk.mEditor), mFlags(rk.mFlags){}
		ScriptSolutionItem& ScriptSolutionItem::operator=( const ScriptSolutionItem& rk ){ mNode=rk.mNode; mSolution=rk.mSolution; mEditor=rk.mEditor; mFlags = rk.mFlags; return *this; }
		ScriptSolutionItem::~ScriptSolutionItem( ){ destroy(); }
		
		void				ScriptSolutionItem::setFlags( size_t flags ){ 
			mFlags = flags; 
			if( mNode )
			{
				mNode->setFlags( Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled );
				mNode->setCheckState( 0, ( mFlags & ItemIsCompilable ) ? Qt::Checked : Qt::Unchecked );
			}
		}
		bool ScriptSolutionItem::isEditorOpen() const{
			return mEditor && mEditor->isVisible();
		}
		void ScriptSolutionItem::openEditor( )
		{
			if( !isEditorOpen() )
			{
				mEditor->setId( mSolution->getTabs()->addTab( *mEditor, getName().c_str() ) );
				mEditor->setVisible( true );
			}
		}
		void ScriptSolutionItem::closeEditor( )
		{
			if( isEditorOpen() )
			{
				save();
				mSolution->getTabs()->removeTab( mSolution->getTabs()->indexOf( *mEditor ) );
				mEditor->setVisible( false );
			}
		}

		void				ScriptSolutionItem::setCurrent( ){ mSolution->getTabs()->setCurrentWidget( *mEditor ); }
		bool				ScriptSolutionItem::isCurrent() const{ return mSolution->getTabs()->currentWidget() == *mEditor; }
		size_t				ScriptSolutionItem::getFlags() const{ return mFlags; }
		bool				ScriptSolutionItem::testFlag( ScriptSolutionItem::Flags flag ){ return ((mFlags&flag)!=NULL); }
		bool				ScriptSolutionItem::testFlags( size_t flags ){ return ((mFlags&flags)!=NULL); }

		bool				ScriptSolutionItem::isModified( ) const{ return mEditor->isModified(); }
		void				ScriptSolutionItem::setModified( bool state ){ mEditor->setModified( state ); }

		ScriptSolutionItem::NodePtr&			ScriptSolutionItem::getNode(){ return mNode; }
		const ScriptSolutionItem::NodePtr&		ScriptSolutionItem::getNode() const{ return mNode; }
		
		ScriptSolutionItem::WidgetPtr&			ScriptSolutionItem::getEditor(){ return mEditor; }
		const ScriptSolutionItem::WidgetPtr&	ScriptSolutionItem::getEditor() const{ return mEditor; }

		ScriptSolutionPtr&			ScriptSolutionItem::getParent(){ return mSolution; }
		const ScriptSolutionPtr&	ScriptSolutionItem::getParent() const{ return mSolution; }

		void				ScriptSolutionItem::save( ){ mEditor->saveScript(); }
		void				ScriptSolutionItem::load( ){ mEditor->loadScript(); }
		
		void				ScriptSolutionItem::setPath( const FilePath& path ){ mEditor->setPath( path ); }
		FilePath			ScriptSolutionItem::getPath() const{ return mEditor->getPath(); }
		
		std::string			ScriptSolutionItem::getDir() const{ return mEditor->getPath().dirName(); }

		std::string			ScriptSolutionItem::getName( bool ext ) const { return mEditor->getName(ext); }
		void				ScriptSolutionItem::setName( const std::string &str ) { 
			boost::filesystem::remove( getPath().str() );
			mEditor->setPath( getDir() + "/" + str ); 
			mEditor->saveScript();
		}
		
		void				ScriptSolutionItem::setContent( const std::string &txt ){ mEditor->setText( txt ); }
		std::string			ScriptSolutionItem::getContent( ) const{ return mEditor->getText( ); }

		bool ScriptSolutionItem::operator == ( const ScriptSolutionItem& other ) const{ return (mNode == other.mNode && mEditor == other.mEditor); }
		bool ScriptSolutionItem::operator != ( const ScriptSolutionItem& other ) const{ return (mNode != other.mNode || mEditor != other.mEditor); }

		ScriptSolutionItem::operator bool() const{ return (mNode && mEditor ); }

		void ScriptSolutionItem::destroy()
		{
			mNode.destroy();
			mEditor.destroy();
		}
	};
};